package com.tl.boot.common.exception;

import com.tl.boot.common.enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * com.tl.boot.common.exception -- BaseException
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/10 12:56
 */
public class BaseException extends RuntimeException {
    /**
     * 异常枚举
     */
    private ResultEnum errorEnum;
    private String msg;


    public ResultEnum getErrorEnum() {
        return errorEnum;
    }

    public void setErrorEnum(ResultEnum errorEnum) {
        this.errorEnum = errorEnum;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, ResultEnum errorEnum, String msg) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.errorEnum = errorEnum;
        this.msg = msg;
    }

    public BaseException(ResultEnum errorEnum) {
        this.errorEnum = errorEnum;
    }

    public String getCode() {
        return errorEnum.getCode();
    }

    @Override
    public String getMessage() {
        if (StringUtils.isBlank(this.msg)) {
            return errorEnum.getMessage();
        } else {
            return String.format(errorEnum.getMessage(), this.msg);
        }
    }

    @Override
    public String toString() {
        return String.format("%s:%s", errorEnum.getCode(), errorEnum.getMessage());
    }
}


